﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ipaidabribe.Code;

namespace ipaidabribe
{
    public partial class Detail : System.Web.UI.Page
    {

        public int _bribeId = 0;
        public string friendlyUrl = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null)
            {
                int id = 0;
                int.TryParse(Request.QueryString["id"].ToString(), out id);
                if (id > 0)
                {
                    _bribeId = id;
                    if (!Page.IsPostBack)
                    {
                        //lay du lieu 1 post
                        var db = new ipaidabribeEntities();
                        var bribe = db.Bribes.Where(t => t.Id == id).FirstOrDefault();
                        if (bribe != null)
                        {
                            //fill du lieu
                            this.Page.Title = HttpUtility.HtmlDecode(bribe.Title.Trim()) + " | Tôi đi hối lộ.com - vì một xã hội minh bạch";
                            //meta tag for facebook
                            HtmlMeta meta = new HtmlMeta();
                            meta.Attributes.Add("property", "og:title");
                            meta.Content = HttpUtility.HtmlDecode(bribe.Title.Trim()) + " | Tôi đi hối lộ.com - vì một xã hội minh bạch";
                            this.Header.Controls.Add(meta);
                            HtmlMeta meta2 = new HtmlMeta();
                            meta2.Attributes.Add("property", "og:description");
                            meta2.Content = HttpUtility.HtmlDecode(bribe.Content.Trim()).Replace(Char.ConvertFromUtf32(13).ToString(), "<br>");
                            this.Header.Controls.Add(meta2);

                            lblTitle.Text = HttpUtility.HtmlDecode(bribe.Title.Trim());
                            lblBribeType.Text = bribe.BribeType.Trim();
                            lblCity.Text = bribe.City.Trim();
                            lblContent.Text = HttpUtility.HtmlDecode(bribe.Content.Trim()).Replace(Char.ConvertFromUtf32(13).ToString(), "<br>");
                            lblDateAgo.Text = Utils.ToAgoDate(Convert.ToDateTime(bribe.DatePaid));
                            lblDatePaid.Text = Convert.ToDateTime(bribe.DatePaid).ToString("dd/MM/yy");
                            lblDepartments.Text = bribe.Departments.Trim();

                            //lblTransaction.Text = bribe.Transaction.Trim();
                            lblTotalComment.Text = bribe.TotalComment.ToString();
                            double d = 0;
                            bool abc = double.TryParse(bribe.AmountPaid, out d);
                            if (abc)
                                bribe.AmountPaid = String.Format("{0:0,0}", d) + " VND";
                            lblAmountPaid.Text = " " + bribe.AmountPaid.ToString();
                            friendlyUrl = "http://" + Request.Url.Authority + Request.RawUrl; // Utils.ToFriendlyUrl(HttpUtility.HtmlDecode(bribe.Title.Trim()), id.ToString());
                        }

                        //lay ra cac comment
                        var _comment = db.Comments.Where(t => t.BribeId == _bribeId).ToList();
                        if (_comment != null)
                        {
                            foreach (var _item in _comment)
                            {
                                if (_item.Title == null)
                                    _item.Title = "Không tên";
                            }
                            //bind comment
                            Repeater1.DataSource = _comment;
                            Repeater1.DataBind();
                        }
                    }
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (_bribeId > 0)
            {
                if (Session["captcha"] != null)
                {
                    var title = txt_name.Text.Trim();
                    var info = txt_addi_info.Value.Trim();
                    var security_code = txt_security_code.Text.Trim();
                    if (title == "")
                    {
                        lblError.Text = "Chưa nhập Tên hiển thị!!!";
                        lblError.Visible = true;
                        return;
                    }
                    if (info == "")
                    {
                        lblError.Text = "Chưa nhập nội dung!!!";
                        lblError.Visible = true;
                        return;
                    }
                    if (security_code == "")
                    {
                        lblError.Text = "Chưa nhập mã bảo vệ!!!";
                        lblError.Visible = true;
                        return;
                    }
                    //kiem tra security_code
                    if (security_code != Session["captcha"].ToString())
                    {
                        //sai ma bao ve
                        lblError.Text = "Nhập sai mã bảo vệ!!!";
                        lblError.Visible = true;
                        return;
                    }
                    //insert db
                    var db = new ipaidabribeEntities();

                    var _comment = new Comment();
                    _comment.BribeId = _bribeId;
                    _comment.Title = HttpUtility.HtmlEncode(title);
                    _comment.Content = HttpUtility.HtmlEncode(info);
                    _comment.DateReport = DateTime.Now;
                    db.AddToComments(_comment);
                    var rs = db.SaveChanges();
                    if (rs > 0)
                    {
                        //
                        txt_name.Text = "";
                        txt_addi_info.Value = "";
                        txt_security_code.Text = "";
                        lblError.Text = "Đăng bình luận thành công!!!";
                        lblError.Visible = true;
                        //lay ra cac comment
                        var lst = db.Comments.Where(t => t.BribeId == _bribeId).ToList();
                        if (lst != null)
                        {
                            //bind comment
                            Repeater1.DataSource = lst;
                            Repeater1.DataBind();
                        }

                        //tăng total comment
                        var _bribe = db.Bribes.Where(t => t.Id == _bribeId).FirstOrDefault();
                        _bribe.TotalComment += 1;
                        db.SaveChanges();
                    }
                }
            }
        }
    }
}
